package com.chaopei.day08;

import java.io.*;

/**
 * @Author Corday 天涯
 * @Date 2022/3/14 11:33
 * 磁盘文件复制
 */
public class MyCopy {
    //    输入输出流,完成磁盘文件的复制
    public static void copyFile(File src, File tar) {
        InputStream in = null;
        OutputStream out = null;
        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(tar);
//            读入的同时,输出数据生成新的文件
            byte[] buffer = new byte[in.available()];
            while (in.read(buffer) != -1) {
                //读取数据到程序中
                String temp = new String(buffer, "gb2312");
                //保存文件中,实现复制功能
                out.write(temp.getBytes());
            }
            out.close();
            in.close();
            System.out.println("文件复制成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

//测试一下
class MyCopyTest {
    public static void main(String[] args) {
        File src = new File("c:/temp/cool.txt");
        File tar = new File("c:/temp/pool.txt");
        MyCopy.copyFile(src, tar);
    }//main
}
